import request from "@/utils/request"

export const addClass = async(params) => request.post("/classes", params)

export const getClass = async() => request.get("/classes")

export const typeClass = async(type) => request.get('/classes?type=' + type)
export const searchClass = async(type, key) => {
    if (type) {
        let res = await typeClass(type)
        if (key) {
            let data = []
            for (var k of res.data) {
                if (k.name.includes(key)) {
                    data.push(k)
                }
            }
            return data
        } else {
            return res.data
        }
    } else {
        let res = await getClass()
        if (key) {
            let data = []
            for (var k of res.data) {
                if (k.name.includes(key)) {
                    data.push(k)
                }
            }
            return data
        }

        return res.data
    }
}
export const getClassById = async(id) => request.get('/classes/' + id)
export const changeClass = async(id, params) => {
    request({
        url: '/classes/' + id,
        method: 'PATCH',
        data: params
    })
}
export const delClass = async(id) => {
    request({
        url: '/classes/' + id,
        method: 'DELETE'
    })
}